package metier;

import java.math.BigInteger;
import java.util.ArrayList;

public class Tuple {

	private ArrayList<BigInteger> tuple ;

	public String message ;
	
	public Tuple(String message){
		this.tuple = new ArrayList<BigInteger>();	
		this.message = message ;
	}
	
	public Tuple(String[] message){
		this.tuple = new ArrayList<BigInteger>();
		for(int i = 0 ; i < message.length ; i++)
			tuple.add(new BigInteger(message[i]));
		
	}
	
	public void makeTuple(){
		for(int i =0 ; i < this.message.length(); i++){
			char ch = this.message.charAt(i);
			int a = (int) ch ;	
			this.tuple.add(new BigInteger(a+""));
		}
		
	}
	
	public void chiffrement(Cle cle_pub){
		for(int i = 0 ; i < this.tuple.size(); i++ ){
			BigInteger p = new BigInteger(this.tuple.get(i).toString()) ;
			p = p.modPow(cle_pub.getM_e(), cle_pub.getM_n());
			this.tuple.remove(i);
			this.tuple.add(i,p);
		}
	}

	public void dechiffrement(Cle cle_priv){
		for(int i = 0 ; i < this.tuple.size(); i++ ){
			BigInteger p = new BigInteger(this.tuple.get(i).toString()) ;
			p=  p.modPow(cle_priv.getM_u(), cle_priv.getM_n());
			this.tuple.remove(i);
			this.tuple.add(i, p);
		}
	}
	
	public String toStringTuple(){
		String rep = "" ;
		for(int i = 0 ; i < this.tuple.size(); i++ ){
			 rep = rep+" "+this.tuple.get(i);
		}
			return rep ;
	}
	
	public String toStringMessage(){
		String rep = "" ;
		for(int i = 0 ; i < this.tuple.size(); i++ ){
			int b = this.tuple.get(i).intValue() ; 
			rep = rep+(char)b ;
		}
			return rep ;
	}
	
	public String[] getTabString(){
		String[] tab = new String[tuple.size()];
		
		for(int i = 0 ; i < this.tuple.size(); i++ ){
			BigInteger b  = this.tuple.get(i) ;
			tab[i] = b.toString();
		}
		
		return tab ;
	}
	
}
